﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Logic.ClarizenSvc;
using Logic.ClarizenObjects;
using System.Reflection;
using Logic.Models;

namespace Logic.ClarizenObjects
{
    public class Task : WorkItem
    {
        public Task(GenericEntity entity)
            : base(entity)
        {

        }

        public Task()
        {
        }

        public GenericEntity TaskType { get; set; }
        [Clarizen]
        public String TaskTypeID
        {
            set { this.TaskType = new GenericEntity { Id = new EntityId { TypeName = "TaskType", Value = value } }; }
            get { return this.TaskType != null && this.TaskType.Id != null ? this.TaskType.Id.Value : null; }
        }
        [Clarizen]
        public List<Document> Documents {get;set;}
        [Clarizen]
        public Email Email { get; set;}

        public static void GetTaskResources(List<WorkItem> items)
        {
            //GetWorkItemResourcesMessage msg = new GetWorkItemResourcesMessage { WorkItemsIds = items.Select(i=>i.ID).ToArray()};
            ////msg.Fields = BaseObject.GetFields<RegularResourceLink>().Select(m=> );

            //var res = new QueryComponent().Execute(new BaseMessage[] { msg });

            //if (res == null)
            //    return;

            //var resources = ((GetWorkItemResourcesResult)res[0]).WorkItemResources;

            EntityQuery msg = new EntityQuery { TypeName = "RegularResourceLink", Fields = BaseObject.GetFields<RegularResourceLink>(), Permissions = PermissionOptions.ObjectAndFields, PermissionsSpecified = true };
            msg.Where = new Or
            {
                Conditions = items.Select(i=> new Compare { LeftExpression = new FieldExpression { FieldName = "WorkItem" }, Operator = Operator.Equal, RightExpression = new ConstantExpression { Value = i.ID } }).ToArray()
            };
                
            var res = new QueryComponent().ExecuteQuery(msg);
            var resources = res.Select(ge => BaseObject.Intitialize<RegularResourceLink>(ge)).ToList();

            items.ForEach(i => i.Resources = resources.Where(r=>r.WorkItem.Id.Value == i.ID.Value).ToList());

            //foreach (var r in resources)
            //{
            //    if (r != null && r.Resources != null)
            //        items.First(i => i.ID.Value == r.WorkItemId.Value).Resources = r.Resources.Select(r_r => BaseObject.Intitialize<RegularResourceLink>((GenericEntity)r_r)).ToList();
            //}
        }

        public void LoadTaskResources()
        {
            EntityQuery msg = new EntityQuery { TypeName = "RegularResourceLink", Fields = BaseObject.GetFields<RegularResourceLink>(), Permissions = PermissionOptions.ObjectAndFields, PermissionsSpecified = true };
            msg.Where = new Compare { LeftExpression = new FieldExpression { FieldName = "WorkItem" }, Operator = Operator.Equal, RightExpression = new ConstantExpression { Value = this.ID } };
            var res = new QueryComponent().ExecuteQuery(msg);
            var resources = res.Select(ge => BaseObject.Intitialize<RegularResourceLink>(ge)).ToList();
            resources.ForEach(r=> r.UpdatedFromTimeSheets = this.ActualEffortUpdatedFromTimesheets);

            this.Resources = resources;
        }

        public static Task GetTask(String taskID, String[] fields = null)
        {
            if (fields == null)
                fields = new String[]
                {
                    "ActualEffort",
                    "ActualEffortUpdatedFromTimesheets",
                    "Description",
                    "DueDate",
                    "Duration",
                    "Importance",
                    "IndividualReporting",
                    "Manager",
                    "Manager.Name",
                    "Name",
                    "Parent",
                    "Parent.Name",
                    "ParentProject",
                    "ParentProject.Name",
                    "PercentCompleted",
                    "RemainingEffort",
                    "StartDate",
                    "State",
                    "TrackStatus",
                    "Work",
                    "TaskType"
                };
            var res = new QueryComponent().RetrieveEntity(new EntityId { Value = taskID, TypeName = "Task" }, fields, true);

            var t = new Task(res);
            t.LoadTaskResources();

            return t;
        }
    }
}